package com.lonely.fx;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.OverrunStyle;
import javafx.scene.control.Tooltip;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

/**
 * @date: 2022-07-25 14:50
 * @author: ztkj-hzb
 * @description: 提示框设置
 **/
public class TooltipSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     * 演示悬浮框的实现
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox();
        hBox.setSpacing(30);
        hBox.setPadding(new Insets(20));

        Button button = new Button("Button");

        //创建提示框
        Tooltip tooltip = new Tooltip();

        //设置该提示框与指定组件进行绑定
        //Tooltip.install(button, tooltip);

        //设置提示框内容
        tooltip.setText("这是一个按钮,天有多高，手游多骚~~~~");

        //设置宽高
        tooltip.setPrefWidth(200);
        tooltip.setPrefHeight(200);

        //设置字体
        //tooltip.setFont(new Font(20));

        //设置自动换行
        //tooltip.setWrapText(true);

        //设置文本对其方式
        tooltip.setTextAlignment(TextAlignment.LEFT);

        //当长度不够时，如何处理多余文本，ELLIPSIS:末尾省略号
        tooltip.setTextOverrun(OverrunStyle.ELLIPSIS);

        //设置提示栏相关鼠标哪个位置展示，默认是左上
        //tooltip.setAnchorLocation(PopupWindow.AnchorLocation.WINDOW_BOTTOM_LEFT);

        VBox vBox = new VBox();
        vBox.getChildren().addAll(new Button("测试"), new ImageView("icons/mla.png"));
        tooltip.setGraphic(vBox);


        hBox.getChildren().addAll(button);
        primaryStage.setScene(new Scene(hBox));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();

        button.setOnMouseMoved(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //tooltip.show(primaryStage);
                System.out.println("button modevd");
                Bounds boundsInLocal = button.getBoundsInLocal();
                Point2D point2D = button.localToScreen(boundsInLocal.getMaxX(), boundsInLocal.getMaxY());
                tooltip.show(button, point2D.getX() - 10, point2D.getY() - 10);
            }
        });


        //根据窗口坐标来决定是否显示跟隐藏
        primaryStage.addEventHandler(MouseEvent.MOUSE_MOVED, event -> {

            //根据当前坐标位置决定是否需要展示还是关闭


            double mouseX = event.getScreenX();
            double mouseY = event.getScreenY();
            boolean isNeedHide = true;
            if (mouseX >= tooltip.getAnchorX() && mouseX <= tooltip.getAnchorX() + tooltip.getPrefWidth()) {
                if (mouseY >= tooltip.getAnchorY() && mouseY <= tooltip.getAnchorY() + tooltip.getPrefHeight()) {
                    isNeedHide = false;
                }
            }

            if (isNeedHide) {
                tooltip.hide();
            }

        });

    }
}
